<?php

namespace app\admin\services;

use think\App;
use think\facade\Request;
use think\Service;
use think\facade\Session;
use app\admin\model\AuthruleModel;
use app\admin\model\AuthgroupModel;
use app\admin\model\AdminUserModel;
use app\admin\model\ClassificationModel;
use app\admin\model\NamemanagModel;
use app\admin\model\ProblemModel;
use think\facade\Config;
class IndexService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        $this->AuthruleModel = new AuthruleModel();
        $this->AuthgroupModel = new AuthgroupModel();
        $this->AdminUserModel = new AdminUserModel();
        $this->ClassificationModel = new ClassificationModel();
        $this->NamemanagModel = new NamemanagModel();
        $this->ProblemModel = new ProblemModel();
        parent::__construct($request, $app);
    }

    public function index()
    {
        $groupid = Session::get('authgroup', 'admin'); //权限组
        $uid = Session::get('uid','admin');
        $authgroup = $this->AuthgroupModel->getRulesById($groupid);
        $grouparr = explode(',', $authgroup['rules']);
        if($uid !=1){
            $authdata = $this->AuthruleModel->getAuthByid($grouparr);
        }else{
            $authdata = $this->AuthruleModel->getadminauth();
        }
        $res = $this->recombine($authdata);
        if (!$res) {
            return $this->_getResponse("NUKNOW_EORRE");
        }
        return $this->_getResponse("SUCCESS",$res);
    }

    /**
     * 权限重组
     */
    public function recombine($authdata, $name = 'children', $navlevel = 1)
    {
        $spread = true;
        $tree = array();
        foreach ($authdata as $v) {
            if ($v['navlevel'] == $navlevel) {
                if ($navlevel == 1) {
                    $arr = [
                        "title" => $v['title'],
                        "icon" => $v['icon'],
                        "spread" => $spread,
                        "href" => $v['name'],
                        $name => []
                    ];
                    $arr[$name] = self::recombine($authdata, $name, $v['id']);
                    if ($spread) {
                        $spread = false;
                    }
                } else {
                    $arr = [
                        "title" => $v['title'],
                        "icon" => $v['icon'],
                        "href" => $v['name']
                    ];
                }
                $tree[] = $arr;
            }
        }
        return $tree;
    }


    /**
     * @param 获取用户职位权限
     */
    public function userauth(){
        $auth = Session::get('position','admin');
       return $auth;
    }


    public function edituser(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['password'])){
            return ["status"=> 1,"msg"=>"密码不能为空"];
        }
        if(empty($post['newpassword'])){
            return ["status"=> 1,"msg"=>"请确认密码"];
        }
        if($post['password']!=$post['newpassword']){
            return ["status"=> 1,"msg"=>"密码不一致"];
        }
        $confighd = Config::get('ADMIN_HEADER');
        $configft = Config::get('ADMIN_FOOTER');
        $password = md5($confighd.$post['password'].$configft);
        $uid = Session::get('uid','admin');
        $data = [
            'password' => $password
        ];
        $this->AdminUserModel->edit($uid,$data);
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 获取负责类型
     */
    public function Version(){
        $username = Session::get('username','admin');
        $tiao = [['username','eq',$username]];
        $type = $this->AdminUserModel->findPw($tiao);
        $typeid = $type['Versiontype'];
        if(Session::get('uid','admin')!=1){
            $where = [['id','in',$typeid]];
        }else{
            $where = [];
        }
        $data = $this->ClassificationModel->datalist($where);
        $array =[];
        foreach ($data as $value){
            $value['id'] = empty($value['id'])?"":$value['id'];
            $value['titlename'] = empty($value['titlename'])?"":$value['titlename'];
            $array[] = $value;
        }
        return $array;
    }


    /**
     * @param 所属平台
     */
    public function manaid(){
        $data = $this->NamemanagModel->datalis();
        return $data;
    }

    /**
     * @param 详情所属
     */
    public function auxlist(){
        $post = Request::param();
        $id = $post['id'];
        $where = [['a.id','eq',$id]];
        $rulst = $this->ProblemModel->auxlist($where);
        return $rulst;
    }
    

    /**
     * @param 获取负责类型
     */
    public function Versiond(){
        $data = $this->ClassificationModel->datalistd();
        $array =[];
        foreach ($data as $value){
            $value['id'] = empty($value['id'])?"":$value['id'];
            $value['titlename'] = empty($value['titlename'])?"":$value['titlename'];
            $array[] = $value;
        }
        return $array;
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data]
        ];
        return $maps[$key];
    }

}